﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiSha.Entity;
using YiSha.Entity.SystemManage;
using YiSha.Enums;
using YiSha.Model;
using YiSha.Model.Param.SystemManage;
using YiSha.Model.Result;
using YiSha.Service;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Business.Cache;
using YiSha.Data.Repository;
using YiSah.Entity.Extension.Systen;

namespace YiSha.Service.SystemManage
{
    public class MenuService : RepositoryFactory
    {
        private MenuCache menuCache = new MenuCache();

        #region 获取数据
        public async Task<TData<List<MenuExtension>>> GetList(MenuListParam param)
        {
            var obj = new TData<List<MenuExtension>>();

            List<MenuExtension> list = await menuCache.GetList();
            list = ListFilter(param, list);

            obj.Data = list.Where(w => w.MenuType <= 2).ToList();
            obj.MsgCode = MsgCode.Success;
            return obj;
        }

        public async Task<TData<List<ZtreeInfo>>> GetZtreeList(MenuListParam param)
        {
            var obj = new TData<List<ZtreeInfo>>();
            obj.Data = new List<ZtreeInfo>();

            List<MenuExtension> list = await menuCache.GetList();
            list = ListFilter(param, list);

            foreach (MenuEntity menu in list)
            {
                obj.Data.Add(new ZtreeInfo
                {
                    id = menu.Id,
                    pId = menu.ParentId,
                    name = menu.MenuName
                });
            }

            obj.MsgCode = MsgCode.Success;
            return obj;
        }

        public async Task<TData<MenuExtension>> GetEntity(string id)
        {
            TData<MenuExtension> obj = new TData<MenuExtension>();
            obj.Data = (MenuExtension)SqlSugarAdapters.GetEntity<MenuEntity>(s => s.Id == id);
            if (obj.Data != null)
            {
                var parentId = obj.Data.ParentId;
                if (parentId.IsEmpty())
                {
                    var parentMenu = SqlSugarAdapters.GetEntity<MenuEntity>(s => s.ParentId == parentId);
                    if (parentMenu != null)
                    {
                        obj.Data.ParentName = parentMenu.MenuName;
                    }
                }
                else
                {
                    obj.Data.ParentName = "主目录";
                }
            }
            obj.MsgCode = MsgCode.Success;
            return obj;
        }

        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(MenuEntity entity)
        {
            TData<string> obj = new TData<string>();
            try
            {
                if (!entity.Id.IsEmpty() && entity.Id == entity.ParentId)
                {
                    obj.Msg = "不能选择自己作为上级菜单！";
                    obj.MsgCode = MsgCode.Fail;
                    return obj;
                }
                if (ExistMenuName(entity))
                {
                    obj.Msg = "菜单名称已经存在！";
                    obj.MsgCode = MsgCode.Fail;
                    return obj;
                }
                if (entity.Id.IsEmpty())
                {
                    await entity.Create();
                    SqlSugarAdapters.Insert(entity);
                }
                else
                {
                    await entity.Modify();
                    SqlSugarAdapters.Update(entity);
                }
                menuCache.Remove();
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }
        private bool ExistMenuName(MenuEntity entity)
        {
            var expression = ExtLinq.True<MenuEntity>();
            expression = expression.And(t => t.Valid);
            if (entity.Id.IsEmpty())
            {
                expression = expression.And(t => t.MenuName == entity.MenuName && t.MenuType == entity.MenuType);
            }
            else
            {
                expression = expression.And(t => t.MenuName == entity.MenuName && t.MenuType == entity.MenuType && t.Id != entity.Id);
            }
            return SqlSugarAdapters.GetList(expression).Count() > 0 ? true : false;
        }
        public async Task<TData> DeleteForm(List<string> ids)
        {
            TData obj = new TData();
            var info = SqlSugarAdapters.GetList<MenuEntity>(s => ids.Contains(s.Id));
            if (info.Count > 0)
            {
                SqlSugarAdapters.Delete<MenuEntity>(s => ids.Contains(s.Id));
            }
            else
            {
                obj.MsgCode = MsgCode.Fail;
                obj.Msg = "当前数据不存在";
            }
            SqlSugarAdapters.Delete<MenuEntity>(s => ids.Contains(s.Id));
            // 清除缓存里面的数据
            menuCache.Remove();
            obj.MsgCode = MsgCode.Success;
            return obj;
        }
        #endregion

        #region 私有方法
        private List<MenuExtension> ListFilter(MenuListParam param, List<MenuExtension> list)
        {
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.MenuName))
                {
                    list = list.Where(p => p.MenuName.Contains(param.MenuName)).ToList();
                }
                if (param.MenuStatus > 0)
                {
                    list = list.Where(p => p.MenuStatus == param.MenuStatus).ToList();
                }
            }
            return list;
        }
        #endregion
    }
}
